<?php

class ParameterTable extends PriorityTable
{
  public function getProductParams($user, $product_id, $parameter)
  {
    return Doctrine::getTable('ParameterProductValue')->createQuery('ppv')
      ->innerJoin('ppv.Translation ppvt')
      ->innerJoin('ppv.Parameter p')
      ->innerJoin('p.ParameterGroup pg')
      ->innerJoin('p.Translation pt')
      ->where('ppvt.lang = ?', array($user->getCulture()))
      ->andWhere('pt.lang = ?', array($user->getCulture()))
      ->andWhere('ppv.product_id = ?', array($product_id))
      ->andWhere('pg.is_primary = 1')
      ->andWhere('pg.id = ?', array($parameter))
      ->execute();
  }
	

	
	public function addParamGroupCriteria( $query, array $paramGroupsPrimeKeys ){
		if( empty($paramGroupsPrimeKeys) )
			$paramGroupsPrimeKeys = array( -11111111 );
			
		$query->andWhereIn('group_id', $paramGroupsPrimeKeys );
		return $this;
	}
	
	
	public function addMultilangCriteria( $query, $multilang ){
		
		if( $multilang == 0 )
			$query->where( '( multilang = ? OR ( type = ? OR type = ?  ) )', 
					array( $multilang, 'MANY_OPTIONS', 'SINGLE_OPTIONS' ) );
		else
			$query->where( '( multilang = ?  )',  $multilang )
					->andWhereNotIn( 'type', array( 'MANY_OPTIONS', 'SINGLE_OPTIONS' )  );			
		
		 return $this;
	}

	public function addParameterCriteria($query, $param){
		$query->andwhere('parameter = ?', $param);

		return $this;
	}


  /**
   * Backend Matrix
   */
  public function getConnectedWith($group_id)
  {
    return $this->createQuery('p')
        ->where('p.group_id = ?', $group_id)
        ->setHydrationMode(Doctrine::HYDRATE_ARRAY)
        ->execute();
  }

  public function getPrimaryGroups()
  {
    return Doctrine::getTable('ParameterGroup')->createQuery('pg')
      ->where('is_primary = 1')
      ->setHydrationMode(Doctrine::HYDRATE_ARRAY)
      ->execute();
  }

  public function getLahenduseGroups($category_id, $user)
  {
    return Doctrine::getTable('ProductGroupVsParameterGroup')->createQuery('pp')
      ->innerJoin('pp.ParameterGroup pg')
      ->innerJoin('pg.Translation pgt')
      ->where('pg.is_primary = 1')
      ->andWhere('pgt.lang = ?', $user->getCulture())
      ->andWhere('pp.prodgroup_id = ?', $category_id)
      ->orderBy('pg.rgt ASC')
      ->setHydrationMode(Doctrine::HYDRATE_ARRAY)
      ->execute();
  }

  public function getFirstParameter($product_group, $user)
  {
    return $this->createQuery('p')
        ->innerJoin('p.Translation pt')
        ->where('pt.lang = ?', $user->getCulture())
        ->andWhere('p.group_id = ?', $product_group)
        ->orderBy('p.pri ASC')
        ->setHydrationMode(Doctrine::HYDRATE_ARRAY)
        ->execute();
  }
}